package com.ctakit.sdk.app.base;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import com.ctakit.sdk.SdkApplication;
import com.ctakit.sdk.app.util.ViewUtil;
import com.ctakit.sdk.app.widget.fragmentmaster.app.MasterActivity;
import com.ctakit.sdk.util.TUtil;
import com.ctakit.sdk.view.ViewInjector;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * Created by andyhome on 2016/4/5.
 */
public abstract class BaseActivity<T extends BasePresenter, E extends BaseModel> extends MasterActivity implements MyActivity {

    public T mPresenter;
    public E mModel;
    public KProgressHUD progressDialog;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            ViewInjector.inject(this);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);
        if (this instanceof BaseView) {
            mPresenter.setVM(this, mModel);
        }

    }

    public void askPermission(String[] permissions, boolean option) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (goBack(null, true)) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

////////start/////////代码 和 baseFragement 基本一样，只是没有多继承，只好复制一份，修改时记得两边都修改

    @Override
    public KProgressHUD getProgressDialog() {
        if (progressDialog == null) {
            progressDialog = KProgressHUD.create(getActivity())
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setLabel("加载中...")
                    .setCancellable(true)
                    .setAnimationSpeed(2);
        }

        return progressDialog;
    }

    @Override
    public void showProgressDialog() {
        getProgressDialog().setLabel("加载中...").show();
    }

    @Override
    public void showProgressDialog(final String msg) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getProgressDialog().setLabel(msg).show();
            }
        }, 0);
    }

    @Override
    public void cancelProgressDialog() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (getProgressDialog().isShowing()) {
                    getProgressDialog().dismiss();
                }
            }
        }, 0);
    }

    @Override
    public void showToastMsg(String msg) {
        // ToastHelper.makeText(getActivity(), msg, ToastHelper.LENGTH_SHORT).setAnimation(R.style.popToast).show();
        Toast toast = Toast.makeText(SdkApplication.app().getApplicationContext(), msg, Toast.LENGTH_SHORT);
        if (toast != null) {
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }


    @Override
    public void alert(String msg) {

    }

    public boolean goBack(View v, boolean isPhoneBack) {
        return false;
    }

    ///////// activity 跳转

    /**
     * 跳转到下一个 activity
     * * Map<String, Serializable> data = new HashMap<String, Serializable>();
     * data.put("mobile", mobile);
     */

    public void gotoActivity(Class clazz, Map<String, Serializable> data) {
        gotoActivity(clazz, data, null);
    }

    public void gotoActivity(Class clazz, Map<String, Serializable> data, Map<String, Parcelable> parcelableMap) {
        ViewUtil.gotoActivity(this, clazz, data, parcelableMap);
    }

    // 跳转到下一个 activity(不传数据)

    /**
     * @param clazz
     */
    public void gotoActivity(Class clazz) {
        gotoActivity(clazz, null);
    }

    // 跳转到下一个 activity
    public void gotoActivityForResult(Class<?> clazz, Map<String, Serializable> data) {
        ViewUtil.gotoActivityForResult(this.getActivity(), clazz, data);
    }

    // 跳转到下一个 activity
    public void bottomToActivity(Class<?> clazz, Map<String, Serializable> data) {
        ViewUtil.bottomToActivity(this.getActivity(), clazz, data);
    }

///////////////end///////////////////


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public MyActivity getMyActivity() {
        return this;
    }

    @Override
    public <T extends View> T findView(int id) {
        return (T) findViewById(id);
    }
}